﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YYTea.MOdel.Model;
using YYTea.ORM;
using YYTea.Services.IServices;
using YYTea.Services.VModel;

namespace YYTea.Services.Services
{
    public class ShoppingCarServices : IShoppingCar
    {
        private readonly YYTeaContext _context;
        public ShoppingCarServices(YYTeaContext context)
        {
            _context = context;
        }

        public async Task<ShoppingCar> AddProducrToShop(ShoppingCarVModel shopVM)
        {
            //throw new NotImplementedException();
            if (shopVM == null)
            {
                return null;
            }
            ShoppingCar shop = new ShoppingCar();
            shop.DateTime = DateTime.Now;
            for (int i = 2; i < shop.SizeNum; i++)
            {
                shop.SizeNum += i;
            }
            shop.UserInfoID = shopVM.UserId;

            //var shopPro = shop.Products;
            //shopPro = await _context.Products.FirstOrDefaultAsync(s => s.Name == shopVM.ProductName);
            //shop.Products = shopPro;
            //shop.Products = _context.Products.FirstOrDefault(s=>s.ProductID == shopVM)

            _context.ShoppingCars.Add(shop);
            await _context.SaveChangesAsync();
            return shop;

        }
        //初始化购物车
        public async Task<ShoppingCar> AddShoppingCar(ShoppingCarVModel shopVModel)
        {
            if (shopVModel == null)
            {
                return null;
            }
            var shop = new ShoppingCar();
            shop.UserInfoID = shopVModel.UserId;
            shop.DateTime = shopVModel.DateTime;
            //if (shopVModel.ProductName == null)
            //{
            //    shop.Products = null;
            //}
            //shop.Products = _context.Products.FirstOrDefault(s => s.Name == shopVModel.ProductName);
            await _context.AddAsync(shop);
            await _context.SaveChangesAsync();
            //return addShop;
            return shop;
        }

        public Task<ShoppingCar> DeleteShoppingCarProduct()
        {
            throw new NotImplementedException();
        }

        public Task<ShoppingCar> DeleteShoppingCarProductById(Guid Id)
        {
            throw new NotImplementedException();
            //ShoppingCar shop = new ShoppingCar();
            //ICollection(shop.Products) = _context.Products.SingleOrDefault(s => s.ProductID == Id);
            //if (shop == null)
            //{ return null; }

            //_context.Products.Remove()
        }

        public Task<ShoppingCar> DeleteShoppingCarProductByName(string Name)
        {
            throw new NotImplementedException();
        }

        public async Task<ShoppingCar> GetShoppingCarById(string id)
        {
            //throw new NotImplementedException();
            if (id == null)
            {
                return null;
            }
            var getshop = await _context.ShoppingCars
                //连接用户表
                .Include(s => s.UserInfos)
                //连接购物表
                //.Include(s => s.Products)
                //搜索条件
                .Where(s => s.UserInfoID == id)
                //第一个数据
                .FirstOrDefaultAsync();
            return getshop;
        }
    }
}
